package demo.DataStrucAndAlgo.Easy_pritice;

import java.util.*;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/10/18-10-18-23:10
 * @Description：demo.DataStrucAndAlgo.Easy_pritice
 */
public class Test5 {
    public static void main(String[] args) {

        Set<Student> set = new TreeSet(new Student());
        set.add(new Student(1,"zhangsan"));
        set.add(new Student(2,"lisi"));
        set.add(new Student(2,"wangwu"));
        System.out.println(set);
    }
}
class Student implements Comparator {
    private String name;
    private int cardId;

    public Student(int cardId, String name) {
        this.name = name;
        this.cardId = cardId;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCardId() {
        return cardId;
    }

    public void setCardId(int cardId) {
        this.cardId = cardId;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", cardId=" + cardId +
                '}';
    }

    @Override
    public int compare(Object o1, Object o2) {
        Student stu1 = (Student) o1;
        Student stu2 = (Student) o2;
        return stu1.getCardId() - stu2.getCardId();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return cardId == student.cardId;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, cardId);
    }
}
